package org.java.item.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.java.item.entity.Sku;

import java.util.List;

/**
 * @author WHQ
 * @version 1.0
 * @date 2022-3-4 0004 下午 3:02
 */
public interface SkuMapper extends BaseMapper<Sku> {
    /**
     * 自定义的减库存的方法
     * @param skuId
     * @param num
     */
    @Update("update shopping_sku set inventory=inventory-#{num} where id=#{skuId}")
    public void cutInventory(Long skuId,Integer num);

    /**
     * 查询全部数据的方法
     * @return
     */
    @Select("select * from shopping_sku")
    public List<Sku> selectAll();

    /**
     * 根据主键删除sku
     * @param skuId
     * @return
     */
    @Delete("delete from shopping_sku where id=#{skuId}")
    int deleteByPrimaryKey(@Param("skuId") Long skuId);

    /**
     * 根据主键查找sku
     * @param id
     * @return
     */
    @Select("select * from shopping_sku where id=#{id}")
    Sku selectByPrimaryKey(@Param("id") Long id);

    /**
     * 添加商品
     * @param sku
     * @return
     */
    @Insert("insert into shopping_sku values(#{id},20,#{title},#{images},#{price},#{inventory},#{indexes},#{ownSpec},#{enable},#{createTime},#{lastUpdateTime})")
    int insertSelective(Sku sku);

    /**
     * 修改商品信息
     * @param sku
     * @return
     */
    @Update("update shopping_sku set title=#{title},images=#{images},price=#{price} where id=#{id}")
    int updateByPrimaryKeySelective(Sku sku);

    /**
     * 订单超时，对库存进行回滚
     * @param skuId
     * @param inventory
     */
    @Update("update shopping_sku set inventory=inventory+#{inventory} where id=#{skuId}")
    void rollbackInventory(@Param("skuId")Long skuId,@Param("inventory")Long inventory);
}
